<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
<div v-if="!loggedIn">
 <button @click="loginPopUp">You are not logged in, click here</button>
</div>
<div v-else>
You are logged in as {{name}}
</div>
      <CurrentTime :time="timeFromServer">  </CurrentTime>
<button @click="refreshTime">Click to refresh time from server</button>
        <p>
      For guide and recipes on how to configure / customize this project,<br>
      check out the
      <a href="https://cli.vuejs.org" target="_blank">vue-cli documentation</a>.
         </p>
    <h3>Installed CLI Plugins</h3>
    <ul>
      <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint" target="_blank">eslint</a></li>
      <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-unit-mocha" target="_blank">unit-mocha</a></li>
    </ul>
    <h3>Essential Links</h3>
    <ul>
      <li><a href="https://vuejs.org" target="_blank">Core Docs</a></li>
      <li><a href="https://forum.vuejs.org" target="_blank">Forum</a></li>
      <li><a href="https://chat.vuejs.org" target="_blank">Community Chat</a></li>
      <li><a href="https://twitter.com/vuejs" target="_blank">Twitter</a></li>
      <li><a href="https://news.vuejs.org" target="_blank">News</a></li>
    </ul>
    <h3>Ecosystem</h3>
    <ul>
      <li><a href="https://router.vuejs.org" target="_blank">vue-router</a></li>
      <li><a href="https://vuex.vuejs.org" target="_blank">vuex</a></li>
      <li><a href="https://github.com/vuejs/vue-devtools#vue-devtools" target="_blank">vue-devtools</a></li>
      <li><a href="https://vue-loader.vuejs.org" target="_blank">vue-loader</a></li>
      <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li>
    </ul>
  </div>
</template>

<script>
import CurrentTime from '@/components/CurrentTime.vue';
import axios from 'axios';
import { mapGetters, mapActions } from 'vuex';

export default {
  name: 'HelloWorld',
  props: {
    msg: String,
  },
  data() {
    return {
      timeFromServer: null,
    };
  },
  components: {
    CurrentTime,
  },
  mounted() {
    this.ensureLogIn();
    this.refreshTime();
  },
  computed: {
    ...mapGetters(['name', 'loggedIn']),
  },
  methods: {
    ...mapActions(['ensureLogIn']),
   /** Trigger a login popup and check that it had some effect*/
   loginPopUp() {
      window.open('/login');
      // nicer way than periodical check ?
      const checker = setInterval(() => {
        this.ensureLogIn();
        if (this.loggedIn) {
          clearTimeout(checker);
        }
      }, 3000);
    },
    refreshTime() {
      axios.get('/api/current_time')
        .then((response) => {
          this.timeFromServer = response.data.current_time;
        })
        .catch((error) => {
          console.error(error);
        });
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
